사이트 내 전체검색
[서버관리] RewriteCond RewriteRule .htaccess 예제
하우코드
https://cmd.kr/server/1149 URL이 복사되었습니다.

본문

.htaccess 테스트 사이트 / Test your htaccess rewrite rules
https://htaccess.madewithlove.com/


자주 사용되는 조건
^  시작문자가 일치해야 한다.
$  종료문자가 일치해야 한다.
OR  RewriteCond 다음에 또 다른 RewriteCond가 있을 경우, 다음 RewriteCond와 logical OR로 조합되도록 설정한다.
R  Substitution이 Absolute URL일 경우, hostname이 서버의 호스트와 일치하는 경우에도 강제로 다시 redirect하도록 한다.
L  rewriting 과정을 이곳에서 종료하도록 한다.


RewriteCond %{HTTP_HOST} ^(www\.aaa\.com|aaa\.com)
RewriteRule (.*)  http://www.bbb.net/$1  [L]

www.aaa.com으로 시작하는 주소만 www.bbb.net로 보내기

RewriteCond %{HTTP_HOST} ^(www\.aaa\.com)
RewriteRule (.*)  http://www.bbb.net/$1  [L]


www.aaa.com과 aaa.com으로 시작하는 주소를 www.bbb.net로 보내기

RewriteCond %{HTTP_HOST} ^(www\.aaa\.com|aaa\.com)
RewriteRule (.*)  http://www.bbb.net/$1  [L]

aaa.com을 www.aaa.com으로 바꾸기

RewriteCond %{HTTP_HOST} ^(aaa\.com)
RewriteRule (.*)  http://www.aaa.com/$1  [L]

aaa.com을 aaa.com/bbb로 보내기
RewriteCond %{HTTP_HOST} ^(aaa\.com|www\.aaa\.com)
RewriteRule (.*)  /bbb/$1  [L]
[L]은 이후에 뒤에 구문이 나오든 그 줄에서 끝낸다는 뜻이다.


aaa.com/bbb를 하위 주소까지 ccc.net로 보내기 (영구 이동)
RewriteRule (.*)  http://www.ccc.net/$1  [R=301,L]
또는
RewriteRule  ^bbb/(.*)  http://www.ccc.net/$1  [R=301,L]
여기서 R=301은 그 주소로 영구 이동하는 것을 나타낸다. 이렇게 하면 도메인 주소를 바꿨을 때에 검색기들이 알아 차리고, 옛 주소를 새 주소로 갱신할 수 있다.

aaa.com이 들어가는 모든 주소(서브도메인 포함)를 bbb.net로 보내기
RewriteCond %{HTTP_HOST} aaa\.com
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.aaa\.com(.*)  http://$1.bbb.net/$2  [L]
RewriteRule ^aaa\.com(.*)  http://bbb.net/$1  [L]

/33처럼 숫자로 이루어진 글 주소와 /entry/가 붙은 제목 주소만 /home/???로 보내려면
RewriteRule ^([0-9]+|(entry/[^/]+)) /home/$1 [R=301,L]


# www 강제로 붙이기.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
# www 가 붙어있으면 떼기.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]



# https 강제이동.
RewriteCond %{SERVER_NAME} =howcode.co.kr [OR]
RewriteCond %{SERVER_NAME} =howcode.co.kr [OR]
RewriteCond %{SERVER_NAME} =howcode.co.kr

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


그누보드 기본 .htaccess

#### 그누보드5 rewrite BEGIN #####
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /main/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^content/([0-9a-zA-Z_]+)$  bbs/content.php?co_id=$1&rewrite=1  [QSA,L]
RewriteRule ^content/([^/]+)/$  bbs/content.php?co_seo_title=$1&rewrite=1      [QSA,L]
RewriteRule ^rss/([0-9a-zA-Z_]+)$  bbs/rss.php?bo_table=$1        [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)$  bbs/board.php?bo_table=$1&rewrite=1      [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/([^/]+)/$ bbs/board.php?bo_table=$1&wr_seo_title=$2&rewrite=1      [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/write$  bbs/write.php?bo_table=$1&rewrite=1    [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/([0-9]+)$  bbs/board.php?bo_table=$1&wr_id=$2&rewrite=1  [QSA,L]
</IfModule>
#### 그누보드5 rewrite END #####




도메인 여러개 있을때 처리 방법

RewriteCond %{HTTP_HOST} ^(esi\.kr|essy\.kr)
RewriteRule (.*) https://esi.kr/esi/$1 [L]

RewriteCond %{HTTP_HOST} ^(howcode\.co\.kr)
RewriteRule (.*) https://howcode.co.kr/main/$1 [L]



flags 다수의 호스트를 매칭하길 원할 경우 OR을 사용해 다음과 같이 설정 할 수 있습니다.
RewriteCond %{HTTP_HOST} ^host1.* [OR]
RewriteCond %{HTTP_HOST} ^host2.* [OR]
RewriteCond %{HTTP_HOST} ^host3.*
RewriteRule ...

하나만
RewriteCond <TestString> <CondPattern> flags
RewriteCond %{HTTP_HOST} ^howcode\.kr$



domain이 test.co.kr이며, port가 80인 경우 모든 url에 대해서 https://test.co.kr로 분기
^test : 첫 문자가 test로 시작
kr$ : 문자열의 끝이 kr

RewriteCond %{HTTP_HOST} ^test\.co\.kr$
RewriteCond %{SERVER_PORT} 80
RewriteRule . https://test.co.kr%{REQUEST_URI} [L]


http://test.co.kr/test/ 요청에 대해 , https://test. co.kr/test2/ 로 분기
( ) : ( ) 안의 문자 또는 문자열을 그룹으로 묶으며, 이 문자그룹은 $N 의 변수로 활용
. : 다수의 한 문자
* : 0개 이상의 문자 또는 문자열- $N : RewriteRule의 패턴 중 N번째 그룹 패턴을 지칭

RewriteCond %{HTTP_HOST} ^test\.co\.kr$
RewriteCond %{SERVER_PORT} 80
RewriteRule /test/(.*) https://test.co.kr/test2/$1 [L]

1. test1.test.co.kr로 들어오는 요청을 http://www.google.com 로 redirect 할 때
RewriteCond %{HTTP_HOST} ^test1.test.co.kr$  // 앞에 ^ 뒤에 $ 는 정확한 매칭이 필요할 경우 넣는다. 빼면 이외에도 적용
RewriteRule . http://www.google.com [L]

2. test1.test.co.kr:26000/ 으로 들어오는 모든 요청에 대해 test1.test.co.kr:26000/session/index.jsp로 redirect 할 때
RewriteCond %{HTTP_HOST} ^test1.test.co.kr:26000$
RewriteRule ^/$ /session/index.jsp [R,L]

3. test1.test.co.kr:26000/session/ 요청에 대해 http://www.google.com 로 redirect 할 때
RewriteCond %{HTTP_HOST} ^test1.test.co.kr
RewriteCond %{SERVER_PORT} 26000$
RewriteCond %{REQUEST_URI} ^/session/$
RewriteRule . http://www.google.com [L]

4. test1.test.co.kr:26000/session/ 요청에 대해 test1.test.co.kr:26000/session/index.jsp 로 redirect 할 때
RewriteCond %{HTTP_HOST} test1.test.co.kr:26000$
RewriteCond %{REQUEST_URI} ^/session/$
RewriteRule . http://test1.test.co.kr:26000/session/index.jsp [R,L]

검색하기
RewriteRule "^search/(.*)$" "/search.php?term=$1" [B,PT]

B,PT 를 사용해야 검색에 / 등의 처리가 가능하다.
RewriteRule "^search/(.*)$" "/search.php?term=$1"
검색어 'x & y / z'가 주어지면 브라우저는이를 'x % 20 % 26 % 20y % 2Fz'로 인코딩하여 'search / x % 20 % 26 % 20y % 2Fz'를 요청합니다. B 플래그가 없으면이 재 작성 규칙은 유효한 URL이 아닌 'search.php? term = x & y / z'에 매핑되므로 search.php?term=x%20&y%2Fz= 로 인코딩됩니다. 의도하지 않은 = .
동일한 규칙에 B 플래그를 설정하면 매개 변수가 출력 URL로 전달되기 전에 다시 인코딩되어 /search.php?term=x%20%26%20y%2Fz 에 올바르게 매핑됩니다 .
httpd는 URL에서 인코딩된 슬래시를 허용하지 않고 슬래시가 있는 경우 404를 반환하므로 이 특정 예제가 작동하려면 AllowEncodedSlashes 를 On 으로 설정해야 할 수도 있습니다 .


RewriteEngine  On
RewriteBase    /mypath/

RewriteCond    %{HTTP_HOST} ^192.168.0.1$
RewriteCond    %{REQUEST_FILENAME} !-f
RewriteCond    %{REQUEST_URI}      !(.*)/$
RewriteCond    %{REQUEST_METHOD}  GET
RewriteRule    ^(.*)$          http://192.168.0.1/mypath/$1/  [L,R=301]

RewriteCond    %{HTTP_HOST} ^public.ip.of.server$
RewriteCond    %{REQUEST_FILENAME} !-f
RewriteCond    %{REQUEST_URI}      !(.*)/$
RewriteCond    %{REQUEST_METHOD}  GET
RewriteRule    ^(.*)$          http://<public.ip.of.server>/mypath/$1/ [L,R=301]


<IfModule mod_rewrite.c>
RewriteEngine On

# 강제로 HTTPS 로 넘김
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 여러 도메인을 한 곳으로
RewriteCond %{HTTP_HOST} ^(example\.com|www\.example\.co\.kr|example\.co\.kr) [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301,NC]

# 브라우저에 강제로 www 붙이기
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ https://www.example.net/$1 [L,R=301,NC]

# 2차도메인을 전부 제거
RewriteCond %{HTTP_HOST} ^([A-Za-z0-9_]+)\.example\.com [NC]
RewriteRule ^(.*)& http://example.com/$1 [L,R]

# www만 제거
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)& http://example.com/$1 [L,R]
</IfModule>

댓글목록

등록된 댓글이 없습니다.

1,139 (1/23P)

Search

Copyright © Cmd 명령어 3.142.35.75